#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
	string reverseStr(string s, int k) {
		for (int i = 0; i < s.size(); i += (2 * k)) {
			// 1. 每隔 2k 个字符的前 k 个字符进行反转
			// 2. 剩余字符小于 2k 但大于或等于 k 个，则反转前 k 个字符
			if (i + k <= s.size()) {
				reverse(s.begin() + i, s.begin() + i + k );
			} else {
				// 3. 剩余字符少于 k 个，则将剩余字符全部反转。
				reverse(s.begin() + i, s.end());
			}
		}
		return s;
	}
};

class Solution_personal {
public:
	void rever(string& s,int l,int r){
		int i = l;
		int j = r - 1;
		while(i < j){
			swap(s[i],s[j]);
			i++;
			j--;
		}
	}
	string reverseStr(string s, int k) {
		for(int i = 0;i < s.size();i += 2 * k){
			if(i + k <= s.size()){
				rever(s,i,i+k);
			}else{
				rever(s,i,s.size());
			}
		}
		return s;
	}
};

class Test{
public:
	void reverseString(string& s,int start,int end){
		int l = start;
		int r = end - 1;
		while(l < r){
			s[l] ^= s[r];
			s[r] ^= s[l];
			s[l] ^= s[r];
			l++;
			r--;
		}
	}

	string reverseStr(string s, int k){
		
		for(int i = 0;i < s.size();i += 2 * k){
			if(i + k <= s.size()){
				reverseString(s,i,i+k);
			}else{
				reverseString(s,i,s.size());
			}
		}
		return s;
	}
};

int main(){
	Test solution;
	string res = solution.reverseStr("abcdefg",2);
	cout << res <<endl;
	return 0;
}
